package com.ruoyi.stuinfo.controller;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.stuinfo.domain.AcademicInfo;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.stuinfo.domain.Attendancerecord;
import com.ruoyi.stuinfo.service.IAttendancerecordService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 考勤记录Controller
 *
 * @author zc
 * @date 2025-05-14
 */
@RestController
@RequestMapping("/stuinfo/attendancerecord")
public class AttendancerecordController extends BaseController
{
    @Autowired
    private IAttendancerecordService attendancerecordService;

    /**
     * 查询考勤记录列表
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:attendancerecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(Attendancerecord attendancerecord)
    {

        String teacherId=getUsername();
        boolean hasH = teacherId.contains("H");
        boolean hasX = teacherId.contains("X");
        if("admin".equals(teacherId))
        {
            //管理员
            startPage();
            List<Attendancerecord> list = attendancerecordService.selectAttendancerecordList(attendancerecord);
            return getDataTable(list);
        }
        else if(hasH)
        {
            //班主任
            teacherId=teacherId.replace("H", "");
            startPage();
            List<Attendancerecord> list = attendancerecordService.selectAttendancerecordList_handteacher(teacherId);
            return getDataTable(list);
        }
        else if(hasX)
        {
            //系主任
            teacherId=teacherId.replace("X", "");
            startPage();
            List<Attendancerecord> list = attendancerecordService.selectAttendancerecordList_Xiteacher(teacherId);
            return getDataTable(list);
        }

        else
        {
            startPage();
            List<Attendancerecord> list = attendancerecordService.selectAttendancerecordList(attendancerecord);
            return getDataTable(list);
        }

    }

    /**
     * 导出考勤记录列表
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:attendancerecord:export')")
    @Log(title = "考勤记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Attendancerecord attendancerecord)
    {

        String teacherId=getUsername();
        boolean hasH = teacherId.contains("H");
        if("admin".equals(teacherId))
        {
            //管理员
            List<Attendancerecord> list = attendancerecordService.selectAttendancerecordList(attendancerecord);
            ExcelUtil<Attendancerecord> util = new ExcelUtil<Attendancerecord>(Attendancerecord.class);
            util.exportExcel(response, list, "考勤记录数据");
        }
        else if(hasH)
        {
            //班主任
            teacherId=teacherId.replace("H", "");
            List<Attendancerecord> list = attendancerecordService.selectAttendancerecordList_handteacher(teacherId);
            ExcelUtil<Attendancerecord> util = new ExcelUtil<Attendancerecord>(Attendancerecord.class);
            util.exportExcel(response, list, "考勤记录数据");
        }
        else
        {
            //系主任
            teacherId=teacherId.replace("X", "");
            List<Attendancerecord> list = attendancerecordService.selectAttendancerecordList_Xiteacher(teacherId);
            ExcelUtil<Attendancerecord> util = new ExcelUtil<Attendancerecord>(Attendancerecord.class);
            util.exportExcel(response, list, "考勤记录数据");
        }
    }

    /**
     * 获取考勤记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:attendancerecord:query')")
    @GetMapping(value = "/{recordId}")
    public AjaxResult getInfo(@PathVariable("recordId") Long recordId)
    {
        return success(attendancerecordService.selectAttendancerecordByRecordId(recordId));
    }

    /**
     * 新增考勤记录
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:attendancerecord:add')")
    @Log(title = "考勤记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Attendancerecord attendancerecord)
    {
        return toAjax(attendancerecordService.insertAttendancerecord(attendancerecord));
    }

    /**
     * 修改考勤记录
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:attendancerecord:edit')")
    @Log(title = "考勤记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Attendancerecord attendancerecord)
    {
        return toAjax(attendancerecordService.updateAttendancerecord(attendancerecord));
    }

    /**
     * 删除考勤记录
     */
    @PreAuthorize("@ss.hasPermi('stuinfo:attendancerecord:remove')")
    @Log(title = "考勤记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{recordIds}")
    public AjaxResult remove(@PathVariable Long[] recordIds)
    {
        return toAjax(attendancerecordService.deleteAttendancerecordByRecordIds(recordIds));
    }


//    @PreAuthorize("@ss.hasPermi('sugsy:sugsy:ym')")
    @GetMapping("/stats")
    public ResponseEntity<Map<String, Object>> getStats() {
         Map<String, Object> stats = attendancerecordService.getDormitoryStats();
         return ResponseEntity.ok(stats);
    }

    @GetMapping("/abnormal-list")
    public ResponseEntity<Map<String, Object>> getAbnormalStudents(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize,
            @RequestParam(required = false) String type) {

        Map<String, Object> result = attendancerecordService.getAbnormalStudents(pageNum, pageSize, type);

        // 确保list不为null
        if (!result.containsKey("list") || result.get("list") == null) {
            result.put("list", Collections.emptyList());
        }

        return ResponseEntity.ok(result);
    }
}
